package controller

import (
	"github.com/gin-gonic/gin"
	"myserver/model"
	"myserver/param"
	"myserver/response"
	"myserver/service"
	"strconv"
)

type SubsidyController struct {
}

var subsidyService = service.SubsidyService{}

func (c *SubsidyController) Router(apiGroup *gin.RouterGroup) {
	apiGroup.GET("/subsidy", c.gets)
	apiGroup.POST("/subsidy", c.posts)
	apiGroup.DELETE("/subsidy", c.deletes)
	apiGroup.PUT("/subsidy", c.puts)
	apiGroup.GET("/subsides", c.getPage)
	apiGroup.GET("/userSubsidy", c.userSubsidys)
}

func (c *SubsidyController) gets(context *gin.Context) {
	var subsidy model.Subsidy
	id, b := context.GetQuery("ID")
	if !b {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
		return
	}
	subsidyId, _ := strconv.ParseUint(id, 10, 64)
	subsidy.ID = uint(subsidyId)
	res := subsidyService.GetSubsidy(subsidy)
	if res.ID != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error())
	}
}

func (c *SubsidyController) posts(context *gin.Context) {
	var subsidy model.Subsidy
	err := context.ShouldBind(&subsidy)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := subsidyService.CreateSubsidy(subsidy)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *SubsidyController) deletes(context *gin.Context) {
	var subsidy model.Subsidy
	err := context.ShouldBind(&subsidy)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := subsidyService.DeleteSubsidy(subsidy)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *SubsidyController) puts(context *gin.Context) {
	var subsidy model.Subsidy
	err := context.ShouldBind(&subsidy)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := subsidyService.UpdateSubsidy(subsidy)
	if res != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *SubsidyController) getPage(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := subsidyService.QuerySubsidy(&pageParam)
	if res.Data != nil && res.Total != 0 {
		response.Success(context, response.Ok().AddData("res", res))
	} else {
		response.Failed(context, response.Error().AddData("res", res))
	}
}

func (c *SubsidyController) userSubsidys(context *gin.Context) {
	var pageParam param.PageParam
	err := context.ShouldBind(&pageParam)
	if err != nil {
		response.Failed(context, response.Error().AddData("msg", "参数解析失败"))
	}
	res := subsidyService.UserSubsidys(pageParam)
	if res.Total != 0 {
		response.Success(context, response.Ok().AddData("subsidies", res.Data).AddData("total", res.Total))
	}else {
		response.Failed(context, response.Error())
	}
}
